package org.ourgrid.broker.business.schedulerdata;

import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.ourgrid.common.logger.NoOpEntityResolver;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class SchedulerData implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String xml;
	
	public SchedulerData() {}
	
	public SchedulerData(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}
	
	public String getCommandType() {
		Document mlDocument = this.getXMlDocument(this.xml);
		String tagName = mlDocument.getDocumentElement().getTagName();
		return tagName;
	}
	
	private Document getXMlDocument(String xml) {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		Document logDocFile = null;
		try {
			DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			docBuilder.setEntityResolver(new NoOpEntityResolver());
			
			Reader reader = new StringReader(xml);
			InputSource s = new InputSource(reader);
			logDocFile = docBuilder.parse(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return logDocFile;
	}
}
